package controller;

import remoteEntity.Log;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import manager.LogManager;

/**
 * LogController
 *
 * JSF Managed backing bean containing model viewing logic for the log viewer.
 * Also applies simple access restriction and logging. page.
 */
@ManagedBean
@RequestScoped
public class LogController {

    @EJB
    LogManager logManager;
    @ManagedProperty(value = "#{userController}")
    private UserController userController;

    public UserController getUserController() {
        return userController;
    }

    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    /**
     * Creates a new instance of LogController
     */
    public LogController() {
    }

    public List<Log> getItems() {
        if (!userController.getIsAdmin()) {
            HttpServletRequest httpServletRequest =
                    (HttpServletRequest) FacesContext.getCurrentInstance().
                    getExternalContext().getRequest();
            String ip = httpServletRequest.getRemoteAddr();
            logManager.logWarning("log", "Unauthorized access from ip " + ip);
            return null;
        } else {
            return logManager.getAllPosts();
        }
    }
}
